package com.ftwj.config.resp;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.List;

/**
 * 存放显示列表信息的包装类。
 * @author Rjk
 * */
public class PageBean<T> implements Serializable {
	
	private static final long serialVersionUID = 1L;

	/**
	 * 当前页码
	 */
	private int curPage;
	/**
	 * 一页显示的记录数
	 */
	private int perPage;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 记录总数
	 */
	private int totalRec;

	/**
	 * 开始页
	 */
	private int beginPage = 1;

	/**
	 * 结束页
	 */
	private int endPage = 1;
	
	/**
	 * 起始行数
	 */
	@JsonIgnore
	private int startIndex;

	/**
	 * 结束行数
	 */
	@JsonIgnore
	private int lastIndex;
	
	/**
	 * 结果集存放List
	 */
	private List<T> recList;
	
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	
	public int getPerPage() {
		return perPage;
	}
	public void setPerPage(int perPage) {
		this.perPage = perPage;
	}
	
	public int getTotalPage() {
		return totalPage;
	}
	
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	public int getTotalRec() {
		return totalRec;
	}
	public void setTotalRec(int totalRec) {
		this.totalRec = totalRec;
	}
	
	public List<T> getRecList() {
		return recList;
	}

	public void setRecList(List<T> recList) {
		this.recList = recList;
	}

	public PageBean() {
	}

	public PageBean(int curPage, int perPage, int totalRec) {
		this.curPage = curPage;
		this.perPage = perPage;
		this.totalRec = totalRec;
		calPageParam();
	}

	/**
	 * 计算总页数
	 */
	public void calTotalPages() {
	   if(totalRec % perPage == 0){
		   this.setTotalPage(totalRec / perPage);
	   }else{
		   this.setTotalPage((totalRec / perPage) + 1);
	   }
	}
	
	/**
	 * 计算开始行数
	 */
	public void calStartIndex() {
		this.startIndex = (curPage - 1) * perPage;
	}

	public int getStartIndex() {
		return startIndex;
	}
	
	/**
	 *  计算结束时候的索引
	 */
	public void calLastIndex() {
	   if (totalRec < perPage) {
		   this.lastIndex = totalRec;
	   }else if((totalRec % perPage == 0) || (totalRec % perPage != 0 && curPage < getTotalPage())){
		   this.lastIndex = curPage * perPage;
       } else if(totalRec % perPage != 0 && curPage == getTotalPage()){ //最后一页
           this.lastIndex = totalRec;
	   } 
	}
	
	public int getLastIndex() {
		return lastIndex;
	}
	
	public void calPageParam(){
		calTotalPages();
		calStartIndex();
		calLastIndex();
	}
	
	/**
	 * 提取jpa分页对象的信息
	 */
	public void convert(Page<T> page) {
		this.setRecList(page.getContent());
		this.setCurPage(page.getPageable().getPageNumber() + 1);
		this.setPerPage(page.getPageable().getPageSize());
		this.setTotalPage(page.getTotalPages());
		this.setTotalRec((int)page.getTotalElements());
	}

	public int getBeginPage() {
		return beginPage;
	}

	public void setBeginPage(int beginPage) {
		this.beginPage = beginPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

}